package com.ruoyi.web.controller.epidemic;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.epidemic.domain.WxCpSysUser;
import com.ruoyi.epidemic.domain.YqInfoCollect;
import com.ruoyi.epidemic.domain.YqLeaveApply;
import com.ruoyi.epidemic.service.IYqInfoCollectService;
import com.ruoyi.epidemic.service.IYqLeaveApplyService;
import com.ruoyi.epidemic.service.RequiredWxOauth2;
import com.ruoyi.epidemic.util.getWeCpService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpUserService;
import me.chanjar.weixin.cp.api.impl.WxCpMessageServiceImpl;
import me.chanjar.weixin.cp.api.impl.WxCpUserServiceImpl;
import me.chanjar.weixin.cp.bean.WxCpUser;
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Controller
@RequestMapping("/epidemic/leaveApply")
@Slf4j
public class leaveApplyController extends BaseController {

    private String prefix = "epidemic/leaveApply";
    @Autowired
    private IYqInfoCollectService yqInfoCollectService;
    @Autowired
    private IYqLeaveApplyService iYqLeaveApplyService;

    @RequiredWxOauth2
    @GetMapping("")
    public String auth(HttpServletRequest request, HttpServletResponse response, ModelMap map) {
        HttpSession session = request.getSession();
        String userId = (String) session.getAttribute(Constants.USER_ID);
        String fillDept = yqInfoCollectService.getFillDept(userId);
//        WxCpUserServiceImpl wxCpUserService = new WxCpUserServiceImpl(wecpservice.getWxCpServiceImpl());
        String name=new String("吴盼盼");
        String position=new String("开发人员");
        String phonenumber=new String("19155858139");
//        try {
//            WxCpUser wxCpUser=wxCpUserService.getById(userId);
//            if(wxCpUser!=null) {
//                name=wxCpUser.getName();
//                position=wxCpUser.getPosition();
//                mobile=wxCpUser.getMobile();
//            }
            map.put("userId",userId);
            map.put("name",name);
            map.put("position",position);
            map.put("phonenumber",phonenumber);
            map.put("deptName",fillDept);
//        }catch(WxErrorException e){
//            e.getMessage();
//        }
        return prefix+"/leaveApply";
    }

    /**
     * 离肥审批单提交
     */
    @Log(title = "离肥审批单提交", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YqLeaveApply yqLeaveApply) {
        AjaxResult ajaxResult = new AjaxResult();
        //防止重提交
        if (yqLeaveApply != null && StrUtil.isNotEmpty(yqLeaveApply.getUserId())) {
            List<YqLeaveApply> list=iYqLeaveApplyService.selectYqLeaveApplyList(yqLeaveApply);
            if (CollUtil.isNotEmpty(list) && list.size() > 0) {
                return AjaxResult.warn(list.get(0).getId()+"");
            }
        }
        return toAjax(iYqLeaveApplyService.insertYqLeaveApply(yqLeaveApply));
    }
}
